<?php
namespace Common\Model;
use Think\Model;
// +----------------------------------------------------------------------
// | Date:2017年2月24日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class AnnualLeaveModel extends Model{
    
    protected $year_start;
    protected $year_end;
    protected $year_cur;
    protected $day_cur;
    
    function __construct(){
        parent::__construct();
        $this->year_cur = date('Y', strtotime(CURRENT_TIME));
        
        $this->year_start = $this->year_cur."-01-01";
        $this->year_end = $this->year_cur."-12-31";
        $this->day_cur = date('Y-m-d',strtotime(CURRENT_TIME));
        
    }
    
    /**
     * 获取全部有调整值的员工工号
     * 
     * date:2018年1月4日
     * author: EK_熊
     */
    public function get_adjust_user(){
        $map['adjust'] = ['gt',0];
        $map['start_date'] = ['egt',$this->year_start];
        $map['end_date'] = ['elt',$this->year_end];
        $ret = $this->where($map)->getField('sn',true);
        return $ret;
        
    }
    
    
//     /**
//      * 创建年假记录,返回剩余的年假天数
//      * @param unknown $sn
//      * @param unknown $join_date
//      * @return boolean|mixed|boolean|unknown|string
//      * date:2017年2月24日
//      * author: EK_熊
//      */
//     function create_annual($sn,$join_date){
//         $curtime = CURRENT_TIME;
//         $year = date('Y',strtotime($curtime));
//         $diffYear = $this->diff_year($curtime, $join_date);//入职年限
//         if ($diffYear >= 1 && $diffYear < 10){
//             $totalDays = 5;
//         }elseif($diffYear >= 10 && $diffYear < 20){
//             $totalDays = 10;
//         }elseif($diffYear >= 20){
//             $totalDays = 15;
//         }else{
//             $totalDays = 0;
//         }
//         $totalmin = $totalDays*8*60;
//         $map = [
//             'sn'=>$sn,
//             'start_date'=>['lt',$curtime],
//             'end_date'=>['egt',$curtime],
//         ];
//         $nextYear = $diffYear+1;
//         $startDate = date('Y-m-d',strtotime("$join_date +$diffYear year"));//年假的开始日期
//         $endDate = date('Y-m-d',strtotime("$join_date +$nextYear year")-86400);//年假的结束日期
//         $isExist = $this->where($map)->find();
//         if (empty($isExist)) {
//             $add = [
//                 'sn'=>$sn,
//                 'total_min'=>$totalmin,
//                 'remain_min'=>$totalmin,
//                 'create_time'=>CURRENT_TIME,
//                 'update_time'=>CURRENT_TIME,
//                 'start_date'=>$startDate,
//                 'end_date'=>$endDate,
//                 'sick_full_total'=>1440,
//                 'sick_full_remain'=>1440,
//                 'sick_half_total'=>960,
//                 'sick_half_remain'=>960,
//             ];
//             $ret = $this->add($add);
//         }else{
//             //总分钟数和在库总分钟数不同，才执行下面的东西
//             if ($totalmin !== intval($isExist['total_min'])){
//                 //要检查是否出现去年的负数情况
//                 $save = [
//                     'total_min'=>$totalmin,
//                     'update_time'=>CURRENT_TIME,
//                     'start_date'=>$startDate,
//                     'end_date'=>$endDate,
//                 ];
                
//                 $ret = $this->where($map)->save($save);            
//             }
//         }
        
//         return $totalmin;
//     }

    /**
     * 登录，创建记录，没有年假，只有全日薪，半日薪，时间是今年至年尾最后一天
     * @param unknown $sn
     * date:2018年1月3日
     * author: EK_熊
     */
    public function update_login($sn,$join_date){
        //检查是否存在
        $is_exist = $this->annual_find($sn);
        if (!$is_exist){
            // 不存在，执行添加创建,新人没年假
            $this->annual_create($sn, 0, $join_date, $this->year_end);
        }
        
    }
    
    /**
     * 检查年假数据是否存在
     * @param unknown $sn
     * date:2018年1月3日
     * author: EK_熊
     */
    public function annual_find($sn){
        $map['sn'] = $sn;
        $map['start_date'] = ['egt',$this->year_start];
        $map['end_date'] = ['elt',$this->year_end];
        $ret = $this->where($map)->find();
        return $ret;       
    }
    
    private function annual_create($sn,$totalmin,$start_date='',$end_date=''){
        //执行数据创建
        $add = [
            'sn'=>$sn,
            'total_min'=>$totalmin,
            'create_time'=>CURRENT_TIME,
            'update_time'=>CURRENT_TIME,
            'start_date'=>$start_date ? $start_date : $this->year_start,
            'end_date'=>$end_date ? $end_date : $this->year_end,
            'sick_full_total'=>1440,
            'sick_full_remain'=>1440,
            'sick_half_total'=>960,
            'sick_half_remain'=>960,
        ];
        $ret = $this->add($add);
        return $ret;        
    }
    
    
    
    
    
    /**
     * 每天执行一次，检查是否有满一、五、十年的员工，
     * 如果有就要更新员工假期
     * 
     * date:2018年1月2日
     * author: EK_熊
     */
    public function update_daily($sn,$join_date){
        $exist = $this->annual_find($sn);
        if (!$exist) return null;//如果没记录，那就返回null，表示什么都不用操作
        $totalmin = $this->formula($join_date);
        if (intval($totalmin) !== intval($exist['total_min'])) {
            $save=[
                'total_min'=>$totalmin,
                'update_time'=>CURRENT_TIME,
                'start_date'=>$this->year_cur.date('-m-d',strtotime($join_date)),
                'end_date'=>$this->year_end,
            ];
          $ret = $this->where(['id'=>$exist['id']])->save($save);
          return $ret;
        }
    }
    
    
    

    
    
    /**
     * 创建本年度的可休年假天数
     * 1月1日执行
     * 本年度可休年假天数 = (上年度实际在职天数/全年天数)
     * 
     * @param unknown $sn
     * @param unknown $join_date
     * date:2017年12月27日
     * author: EK_熊
     */
    public function update_yearly($sn,$join_date){
        $cur_year = $this->year_cur;
        $isExist = $this->annual_find($sn);
        
        if ($isExist) {
           return false;//存在数据，不需要创建
        }
        $totalmin = $this->formula($join_date);
        $ret = $this->annual_create($sn, $totalmin);
        return $ret;

    }
    
    /**
     * 计算获取，年假分钟数，即年假天数
     * 公式计算：本年度可休年假天数 = (上年度实际在职天数/全年天数)*入职年限计算所得天数【5,10,15】
     * 
     * @param unknown $join_date   入职时间
     * @param unknown $totalDays   
     * date:2018年1月3日
     * author: EK_熊
     */
    public function formula($join_date){
        
        $diff = $this->diff_year(CURRENT_TIME, $join_date);//入职年限
        if ($diff > 0 && $diff < 10){
            $totalDays = 5;
        }elseif($diff >= 10 && $diff < 20){
            $totalDays = 10;
        }elseif($diff >= 20){
            $totalDays = 15;
        }else{
            $totalDays = 0;
        }
        
        $last_year = intval($this->year_cur)-1;
        $join_date_year = intval(date('Y',strtotime($join_date)));
        
        if ($last_year == $join_date_year) {
            $ac_days = ((strtotime("$last_year-12-31")-strtotime($join_date))/86400)+1;//上年度的实际在职天数
        }else{
            $ac_days = ((strtotime("$last_year-12-31")-strtotime("$last_year-01-01"))/86400)+1;//上年度的实际在职天数
        }
        
        $last_year_all_days = ((strtotime("$last_year-12-31")-strtotime("$last_year-01-01"))/86400)+1;//上年度的总天数
        $coefficient = round($ac_days/$last_year_all_days,2);
        
        if ($coefficient <= 0) {
            return 0;
        }
        
        $totalmin = $totalDays*$coefficient*8*60;//年假天数
        return $totalmin;
    }
              
    
    public function update($sn,$key,$value){
        $map['sn'] = $sn;
        $map['year'] = date('Y');
        $updateAnnual = $this->where($map)->setDec($key,$value);
        if (!$updateAnnual){
            $this->error = '数据修改失败！';
            return false;
        }
        return true;
    }
    

    
    /**
     * 计算两个日期的年份差,返回年假天数
     * @param unknown $new
     * @param unknown $old
     * @param string $tags
     * date:2017年2月24日
     * author: EK_熊
     */
    function diff_year($new,$old){
        $new = strtotime($new);
        $old = strtotime($old);
        
        $diff = floor(($new-$old) / 60 / 60 / 24 / 365);
        
        return $diff;
    }
    
    /**
     * 剩余的可申请病假数：  剩余可用数-待申请的分钟数-当前申请的分钟数
     * @param unknown $sn
     * @param unknown $sickcode
     * @param unknown $postmin
     * date:2017年6月27日
     * author: EK_熊
     */
    function remain_sick($sn,$sickcode){
        $curDay = date('Y-m-d');
        $map_annual['sn']=$sn;
//         $map_annual['start_date'] = ['elt',$curDay];
//         $map_annual['end_date'] = ['egt',$curDay];
        $annualInfo = $this->where($map_annual)->order('start_date DESC')->field('sick_full_remain,sick_half_remain')->find();
        if ($sickcode == '109') $ret = $annualInfo['sick_full_remain'];
        if ($sickcode == '110') $ret = $annualInfo['sick_half_remain'];
        
        //检查带审核是否有数据存在
        $min_waiting = M('Apply')->where(['status'=>0,'sn'=>$sn,'vacate_type'=>$sickcode])->sum('total_min');
        $min_waiting = $min_waiting ? $min_waiting:0;
        $ret = intval($ret) - $min_waiting; 

        
        return $ret;
    }
    
    /**
     * 年假剩余数量
     * @param unknown $sn
     * date:2017年8月29日
     * author: EK_熊
     */
    function remain_annual($sn){
        $remian = $this->where(['sn'=>$sn])->order('start_date DESC')->field('(total_min - consume_min) as remain_min,(adjust-adjust_consume) as remain_adjust')->find();

        //带审核状态数据
        $min_wait_in_apply = D('Apply')->min_wait_in_apply($sn,103);
        $remian['remain_adjust'] = intval($remian['remain_adjust']);
        $remian['remain_adjust'] = $remian['remain_adjust'] >0 ? $remian['remain_adjust']:0;
        $ret['total'] = $remian['remain_adjust']+intval($remian['remain_min']);
        
        if ($remian['remain_adjust'] > 0 && $min_wait_in_apply == 0){
            $ret['adjust'] = $remian['remain_adjust'];
        }
        $ret['total'] = $ret['total']-$min_wait_in_apply;
        $ret['total'] = $ret['total'] < 0 ? 0 :$ret['total'];
        return $ret;
    }
    
}